#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2019
# @Author  : linhu (hlf0312@gmail.com)
# @Link    : TBD
# @Version : $Id$

from openpyxl import Workbook
from openpyxl import load_workbook
from collections import namedtuple
from time import strftime, localtime

from FeatureBuildsSchedule import buildFeatureBuildsTimeline, getCurrentFB
from JiraIssueCommon import buildJiraIssues

def isProblematicIssue(jiraIssue):
    # print(jiraIssue.itemId)
    if jiraIssue.startFb != 'Null' and jiraIssue.startFb > jiraIssue.endFb:
        jiraIssue.problematicReasons.append('Start FB > End FB')
    if jiraIssue.team == 'Null':
        jiraIssue.problematicReasons.append('Team is Null')
    if jiraIssue.status != 'Obsolete' and jiraIssue.fbCommitStatus == 'Null':
        jiraIssue.problematicReasons.append('FB Commit status is Null')
    if jiraIssue.fbCommitStatus == 'Not Committed' and (jiraIssue.stretchReason == 'Null' or "stretch goal" not in jiraIssue.stretchReason.lower()):
        jiraIssue.problematicReasons.append('Not Committed without stretch reason with <stretch goal> keywords')
    if jiraIssue.status != 'Done' and jiraIssue.status != 'Obsolete' and jiraIssue.delayReason == 'Null' and jiraIssue.delayExplanation == 'Null' and today >= currentFB.getEndDate():
        jiraIssue.problematicReasons.append('Not Done without reason')
    if jiraIssue.status == 'Obsolete' and (jiraIssue.endFb != 'Null' or jiraIssue.remainEstimate != 0):
    	jiraIssue.problematicReasons.append('Obsoleted issue with efforts or endFb')
    if jiraIssue.status == 'Open':
    	jiraIssue.problematicReasons.append('Status is open')
    if jiraIssue.assignee in ['Hu, Zhifeng (NSB - CN/Hangzhou)', 'Yao, Wenrui (NSB - CN/Hangzhou)', 'Wang, Dongdong (NSB - CN/Hangzhou)', \
    'Chen, Weidong 3. (NSB - CN/Hangzhou)', 'Hu, Lingfeng (NSB - CN/Hangzhou)', 'Li, Chenghong (NSB - CN/Hangzhou)']:
        jiraIssue.problematicReasons.append('No Assignee or wrong assignee')
    if jiraIssue.competenceArea == 'Null':
        jiraIssue.problematicReasons.append('No Competence Area')
    if jiraIssue.reporter not in ['Hu, Zhifeng (NSB - CN/Hangzhou)', 'Yao, Wenrui (NSB - CN/Hangzhou)', 'Wang, Dongdong (NSB - CN/Hangzhou)', \
    'Chen, Weidong 3. (NSB - CN/Hangzhou)', 'Hu, Lingfeng (NSB - CN/Hangzhou)', 'Cao, Bin 1. (NSB - CN/Hangzhou)', 'Li, Chenghong (NSB - CN/Hangzhou)']:
        jiraIssue.problematicReasons.append('No reporter or wrong reporter')

    if len(jiraIssue.problematicReasons) != 0:
        # problematicIssues.append(jiraIssue)
        return True
    else:
    	return False

def getProblematicIssues(jiraIssues):
	problematicIssues = []
	for jiraIssue in jiraIssues:
		if isProblematicIssue(jiraIssue):
			problematicIssues.append(jiraIssue)
	sortedProblematicIssues = sorted(problematicIssues, key=lambda x: x.reporter)

	# for problematicIssue in sortedProblematicIssues:
	# 	print('%s: %s %s %s' %(problematicIssue.reporter, problematicIssue.key, problematicIssue.itemId, problematicIssue.problematicReasons))
	return sortedProblematicIssues


if __name__ == '__main__':
	today = strftime("%Y-%m-%d", localtime())
	# now = datetime.now()
	# today = '{:0>2s}'.format(str(now.year)) + '-' + '{:0>2s}'.format(str(now.month)) + '-' + '{:0>2s}'.format(str(now.day))
	# print(today)
	featureBuildsSchedule = buildFeatureBuildsTimeline()
	currentFB = getCurrentFB(today, featureBuildsSchedule)
	print('current FB is %s' %currentFB.getFB())

	toBeValidatedExcelFile = '1906_20190530174046.xlsx'
	print('input file is %s' %toBeValidatedExcelFile)
	jiraIssues = buildJiraIssues(toBeValidatedExcelFile)
	issues = getProblematicIssues(jiraIssues)
	for problematicIssue in issues:
		print('%s: %s %s %s' %(problematicIssue.reporter, problematicIssue.key, problematicIssue.itemId, problematicIssue.problematicReasons))